package common;

import java.io.File;
import java.io.FileWriter;

public class Logging {
	/**
	 * logs the data String to the file fileName in the folder logs/
	 * @param data
	 * @param fileName
	 * @return whether logging was successful
	 */
	public static boolean logData(String data, String fileName){
		try{
			File file = new File("logs");
			if(file.isFile()){
				System.err.println("data generation not possible: file logs exists (should be a folder).");
			} else {
				if(!file.isDirectory()){
					file.mkdir();
				}
				if(writeFile(data, "logs/" + fileName)){
					return true;
				}
			}
		}catch(Exception e){
			System.err.println(e.getMessage());
		}
		return false;
	}
	
	public static boolean writeFile(String data, String fileAndPath){
		try{
			FileWriter w = new java.io.FileWriter(fileAndPath);
			w.write(data);
			w.flush();
			w.close();
		} catch(Exception ex){
			ex.printStackTrace();
			return false;
		}
		return true;
	}
}
